import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:test01/okhttp_test/pojo/saveBasicFitnessInfo_pojo.dart';
import 'package:test01/okhttp_test/util/request_http.dart';

import 'train_page.dart';

/*
*
* 登录界面-------------锻炼部位
*
*
* */

class PositionPage extends StatefulWidget {
  const PositionPage({Key key}) : super(key: key);

  @override
  _PositionPageState createState() => _PositionPageState();
}

class _PositionPageState extends State<PositionPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar:  AppBar(
          leading: GestureDetector(
            onTap: (){
              Navigator.pop(context);
            },
            child: Icon(
              Icons.arrow_back_ios,
              size: 28,
              color: Colors.black,
            ),
          ),
          backgroundColor: Colors.white,
          elevation: 0,
          automaticallyImplyLeading: true,
        ),
        body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(left: 41, top: 42.5),
                child: Text(
                  "有重要想要训练的部位吗",
                  style: TextStyle(
                      fontSize: 23, color: Color.fromRGBO(17, 17, 17, 1)),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 41, top: 12),
                child: Text(
                  "可选全身或1-2个重点部位",
                  style: TextStyle(
                      fontSize: 14, color: Color.fromRGBO(51, 51, 51, 1)),
                ),
              ),
              PositionList(),


            ]));
  }
}


class PositionList extends StatefulWidget {
  const PositionList({Key key}) : super(key: key);

  @override
  _PositionListState createState() => _PositionListState();
}

///类型选择
class _PositionListState extends State<PositionList> {
  //初始化数据
  List position_list=["全身","颈肩","腹部","腰部","手臂","腿部","臀部"];
  //数据被单击的下标
  int click_index;
  //选择除开身体之外的两个部位
  List clickindexList=[];

  SaveBasicFitnessInfo _saveBasicFitnessInfo=SaveBasicFitnessInfo();

  void goalsClick(int clickindex){
    if(clickindex!=0){
      if(clickindexList!=null){
        if(clickindexList.length<2){
          clickindexList.insert(0,clickindex);
          setState(() {});
        }else{
          clickindexList.removeLast();
          clickindexList.insert(0,clickindex);
          setState(() {});
        }
      }
    }else{
      clickindexList=[];
      click_index=clickindex;
      setState(() {});
    }
  }



  @override
  Widget build(BuildContext context) {
    return  Column(
      children: <Widget>[
    Container(
    margin: EdgeInsets.only(top: 62,left: 41),
    width: MediaQuery.of(context).size.width-82,
    child:Wrap(
    spacing: 24,
    runSpacing: 24,
    children: List.generate(position_list.length, (index) {
    return GestureDetector(
    ///获取点击的index
    onTap: (){
    goalsClick(index);
    setState(() {});
    },
    child:clickindexList.isEmpty?
    (click_index!=null?(click_index==index?Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    color: Color.fromRGBO(17, 17, 17, 1),
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    ),
    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(255, 255, 255, 1)),),
    ): Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    border: Border.all(width: 1,color: Color.fromRGBO(204, 204, 204, 1)),
    ),

    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(153, 153, 153, 1)),),
    )):Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    border: Border.all(width: 1,color: Color.fromRGBO(204, 204, 204, 1)),
    ),

    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(153, 153, 153, 1)),),
    )):
    (clickindexList.length>1?(clickindexList[0]==index||clickindexList[1]==index?
    Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    color: Color.fromRGBO(17, 17, 17, 1),
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    ),
    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(255, 255, 255, 1)),),
    ):
    Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    border: Border.all(width: 1,color: Color.fromRGBO(204, 204, 204, 1)),
    ),

    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(153, 153, 153, 1)),),
    )
    ):(clickindexList[0]==index?
    Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    color: Color.fromRGBO(17, 17, 17, 1),
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    ),
    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(255, 255, 255, 1)),),
    )
        :Container(
    width: (MediaQuery.of(context).size.width-82-48)/3,
    height:44,
    alignment: Alignment.center,
    decoration: BoxDecoration(
    borderRadius: BorderRadius.all(Radius.circular(3.0)),
    border: Border.all(width: 1,color: Color.fromRGBO(204, 204, 204, 1)),
    ),

    child: Text("${position_list[index]}",style: TextStyle(fontSize: 16,color: Color.fromRGBO(153, 153, 153, 1)),),
    )))
    );

    }),
    ),
    ),
        GestureDetector(



          onTap: () async{
            if(clickindexList!=null){
              _saveBasicFitnessInfo.keyParts=clickindexList;
            }else{
              _saveBasicFitnessInfo.keyParts[0]="0";
            }
            bool xx=await saveBasicFitnessInfo(_saveBasicFitnessInfo,"/appuser/web/user/saveBasicFitnessInfo");
            if(xx){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return TrainPage();
              }));
            }
          },



          child: Container(
            alignment: Alignment.center,
            height: 41,
            decoration: BoxDecoration(
              color: Color.fromRGBO(17, 17, 17, 1),
              borderRadius: BorderRadius.all(Radius.circular(3.0)),
            ),
            width: MediaQuery.of(context).size.width - 82,
            margin: EdgeInsets.only(left: 41, top: 62),
            child: Text(
              "下一步",
              style: TextStyle(
                  fontSize: 16, color: Color.fromRGBO(255, 255, 255, 1)),
            ),
          ),
        ),
      ],
    );
  }
}
